package com.j.thread;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadShareData {


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        MyData myData = new MyData(100);

        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myData.add();

                }
            }).start();
        }

        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myData.dec();

                }
            }).start();
        }
        System.out.println(myData.j);
    }
}


class  MyData{
    int j;
    public synchronized void add(){
        j++;
        System.out.println(Thread.currentThread().getId() + "执行了+1");

    }

    public synchronized void dec() {
        j--;
        System.out.println(Thread.currentThread().getId() + "执行了-1");
    }
    public MyData(int j) {
        this.j = j;
    }

}